# Copyright (c) 2022 AlgoIdeas <yu19881234@163.com>.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#!/usr/bin/env sh

root_src_dir=$(pwd)
pushd ${root_src_dir}

# boot.img
rm -rf ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/rpi4boot
rm -rf ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/boot.img
rm -rf ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/boot.img.tmp
cp ${root_src_dir}/device/board/raspberrypi/raspberrypi_4b/bootloader/rpi4boot ${root_src_dir}/out/raspberrypi_4b/packages/phone/images -a
mkdir -p ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/rpi4boot/overlays
cp ${root_src_dir}/out/kernel/src_tmp/linux-5.10/arch/arm/boot/zImage ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/rpi4boot -rf
cp ${root_src_dir}/out/kernel/src_tmp/linux-5.10/arch/arm/boot/dts/bcm2711-rpi-4-b.dtb ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/rpi4boot -rf
cp ${root_src_dir}/out/kernel/src_tmp/linux-5.10/arch/arm/boot/dts/overlays/vc4-fkms-v3d.dtbo ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/rpi4boot/overlays -rf
cp ${root_src_dir}/out/kernel/src_tmp/linux-5.10/arch/arm/boot/dts/overlays/rpi-ft5406.dtbo ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/rpi4boot/overlays -rf
cp ${root_src_dir}/out/kernel/src_tmp/linux-5.10/arch/arm/boot/dts/overlays/rpi-backlight.dtbo ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/rpi4boot/overlays -rf
dd if=/dev/zero of=${root_src_dir}/out/raspberrypi_4b/packages/phone/images/boot.img.tmp bs=1M count=64 > /dev/null
mkfs.vfat -n "BOOT" ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/boot.img.tmp > /dev/null
mcopy -i ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/boot.img.tmp ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/rpi4boot/* ::/
mv ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/boot.img.tmp ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/boot.img
${root_src_dir}/device/board/raspberrypi/raspberrypi_4b/common/tools/linux/make_sdcard_image.py  --output ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/OpenHarmony.img ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/boot.img ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/system.img ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/vendor.img ${root_src_dir}/out/raspberrypi_4b/packages/phone/images/userdata.img

popd
